<?PHP
/* $Id: search.php 7 2009-03-24 20:25:52Z ronan $ */

session_start();
//getting the user's access to see if they are allowed
include("includes/paxsql.php");
mysql_close();

include 'includes/database.php';
include 'includes/library.php';

function listMains() {
	$query = "SELECT characters.charID, characters.name FROM characters
		LEFT JOIN alts ON characters.charID = alts.charID
		WHERE characters.name LIKE '%{$_POST['search']}%'
		AND alts.charID IS NULL
		AND characters.inCorp = 1
		ORDER BY characters.name";
	$results = mysql_query($query);
	
	if (mysql_num_rows($results) == 0)
		echo "<div class=\"blank\" style=\"padding-left: 30px;\">No characters found.</div>";
	else {
		while ($char = mysql_fetch_array($results)) {
			echo "<div class=\"blank\" style=\"padding-left: 30px;\">";
			renderName($char['charID']);
			echo "</div>";
		}
	}
}

function listAlts() {
	$query = "SELECT characters.charID AS altID, characters.name AS altName,
		altOf.name AS mainName, altOf.charID AS mainID, characters.inCorp
		FROM characters
		LEFT JOIN alts ON characters.charID = alts.charID 
		LEFT JOIN characters AS altOf ON alts.altOf = altOf.charID
		WHERE characters.name LIKE '%{$_POST['search']}%'
		AND alts.charID IS NOT NULL
		AND characters.inCorp = 1
		ORDER BY characters.name";
	$results = mysql_query($query);
	
	if (mysql_num_rows($results) == 0)
		echo "<div class=\"blank\" style=\"padding-left: 30px;\">No characters found.</div>";
	
	else {
		while ($alts = mysql_fetch_array($results)) {
			echo "<div class=\"blank\" style=\"padding-left: 30px;\">
				{$alts['altName']} <i>is an alt of <a href=\"characters.php?id={$alts['mainID']}\">{$alts['mainName']}</a></i>
				<a href=\"isAlt.php?charID={$alts['altID']}&altOf={$alts['mainID']}&del=1\"><img src=\"images/notalt.gif\"></a>
				</div>";
		}
	}
}

if($access > 1)
{
echo("
<html>
<head>
<link rel='stylesheet' type='text/css' href='main.css'>
<title>snowflake</title>");

include('includes/javascript.php');
echo("
</head>

<body>

<div id='isAltWin' style='display: none;'></div>
<div id='flagWin' style='display: none;'></div>

<div id='wrapper'>

	<div id='header'><a href='/'>snowflake</a></div>

	<div id='navi'>");

include('includes/navi.php');

echo("</div>

	<div id='main'>
		<h1>search</h1>
		<hr>
		<h3>Mains matching... (".stripslashes($_POST['search']).")</h3>");

listMains();
echo("
		<h3>Alts matching... (".stripslashes($_POST['search']).")</h3>");

listAlts();
echo("
	</div>
	
</div>

</body>

</html>");
}
//they do not belong here, they get the boot
else
{
	header("Location: http://".$_SERVER['HTTP_HOST']."/index.php");
}
?>
